package com.sl.hx.shop.ui.classify.fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.Rect;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sl.hx.shop.R;
import com.sl.hx.shop.base.BaseFragment;
import com.sl.hx.shop.base.BasePresenter;
import com.sl.hx.shop.bean.GoodsClassify;
import com.sl.hx.shop.ui.classify.activity.SearchResultActivity;
import com.sl.hx.shop.ui.classify.adapter.ClassifyListAdapter;
import com.sl.hx.shop.ui.home.fragment.GoodsListFragment;
import com.sl.hx.shop.utils.ScreenUtil;
import com.sl.hx.shop.utils.SearchHistoryUtil;
import com.sl.hx.shop.widget.CustomGridLayoutManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.Unbinder;

//分类页fragment
public class ClassifyListFragment extends BaseFragment {


    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.tvname)
    TextView tvname;

    private String mId;
    private GoodsClassify mGoodsClassify;
    private ClassifyListAdapter mClassifyListAdapter;
    private List<GoodsClassify.SubBean> subBeans = new ArrayList<>();

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int getViewLayoutId() {
        return R.layout.fragment_classifylist;
    }

    @Override
    protected void initView(View mView) {
        //接收数据
        if (getArguments() != null) {
            mGoodsClassify = getArguments().getParcelable("goodsClassify");
        }
        //fragment标题
        tvname.setText(mGoodsClassify.getName());
        //初始化列表
        initRecycle();

    }

    private void initRecycle() {
        subBeans=mGoodsClassify.getSub();
        mClassifyListAdapter = new ClassifyListAdapter(subBeans);
        CustomGridLayoutManager customGridLayoutManager = new CustomGridLayoutManager(mContext,3);
        customGridLayoutManager.setScrollEnabled(false);//禁止滑动
        mRecyclerView.setLayoutManager(customGridLayoutManager);

        mRecyclerView.addItemDecoration(new MarginDecoration(mContext));
        mRecyclerView.setAdapter(mClassifyListAdapter);
        mClassifyListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                SearchHistoryUtil.saveHistory(mClassifyListAdapter.getItem(position).getName());
                Intent intent = new Intent(getActivity(), SearchResultActivity.class);
                intent.putExtra("searchTitle",mClassifyListAdapter.getItem(position).getName());
                startActivity(intent);
            }
        });
    }


    //recycleview item设置间距
    class MarginDecoration extends RecyclerView.ItemDecoration {
        private int margintop,marginleft,marginright;

        public MarginDecoration(Context context) {
            margintop = ScreenUtil.dp2px(context,8);
            marginleft = ScreenUtil.dp2px(context,20);
            marginright = ScreenUtil.dp2px(context,4);
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                outRect.set(marginleft,0,marginleft,0);
        }
    }

}
